package cn.itcast.ssh.web.action;

import cn.itcast.ssh.bean.TaskView;
import cn.itcast.ssh.domain.ApproveInfo;
import cn.itcast.ssh.domain.Form;
import cn.itcast.ssh.domain.FormTemplate;
import cn.itcast.ssh.service.FormFlowService;
import cn.itcast.ssh.service.FormTemplateService;
import cn.itcast.ssh.utils.SessionContext;
import cn.itcast.ssh.utils.ValueContext;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 1. 审批流程的第一个环节都是提交申请，后面的环节都是审批。审批环节的数量和审批人数量由具体的流程确定，这就是不同的审批流程。
 * 2. 所以画流程图的时候，第一个任务都应是提交申请，办理人就是本次申请的申请人，所以第一个环节的assignne=${form.employee.name}，
 * 提交的时候把form设置为流程变量，并要启动流程实例完成第一个任务。
 * @author xiongshiyan at 2021/3/16 , contact me with email yanshixiong@126.com or phone 15208384257
 */
public class FormFlowAction extends BaseAction<Object>{
    private FormTemplateService formTemplateService;
    private FormFlowService formFlowService;

    private Long formTemplateId;
    private File upload;


    private boolean arrprove;
    private String comment;
    private Long formId;
    private String taskId;

    /**起草申请（表单模板列表）*/
    public String formTemplateList(){
        List<FormTemplate> formTemplateList = formTemplateService.findAll();
        ValueContext.putValueContext("formTemplateList", formTemplateList);
        return "formTemplateList";
    }
    /**提交申请页面*/
    public String submitUI(){
        return "submitUI";
    }
    /**提交申请*/
    public String submit(){
        Form form = new Form();
        form.setFormTemplate(formTemplateService.getById(formTemplateId));
        form.setPath(saveUploadFile(upload));
        form.setEmployee(SessionContext.get());
        form.setApplyTime(new Date());
        //提交申请，并刚开始流转
        formFlowService.submit(form);
        return "toMyApplicationList";
    }
    /**我的申请查询*/
    public String myApplicationList(){
        return "myApplicationList";
    }


    /////////////////////////////////////////////////////////////////////
    /**待我审批（我的任务列表）*/
    public String myTaskList(){
        List<TaskView> taskViewList = formFlowService.getMyTaskViewList(getCurrentUser());
        ValueContext.putValueContext("taskViewList", taskViewList);
        return "myTaskList";
    }
    /**审批处理页面*/
    public String approveUI(){
        return "approveUI";
    }
    /**审批处理*/
    public String approve(){
        ApproveInfo approveInfo = new ApproveInfo();
        approveInfo.setEmployee(getCurrentUser());
        approveInfo.setApproveTime(new Date());

        approveInfo.setApproval(arrprove);
        approveInfo.setComment(comment);

        approveInfo.setForm(formFlowService.getById(formId));
        formFlowService.approve(approveInfo, taskId);
        return "toMyTaskList";
    }
    /**查看流转记录*/
    public String approveHistory(){
        return "approveHistory";
    }

    public FormFlowService getFormFlowService() {
        return formFlowService;
    }

    public void setFormFlowService(FormFlowService formFlowService) {
        this.formFlowService = formFlowService;
    }

    public FormTemplateService getFormTemplateService() {
        return formTemplateService;
    }

    public void setFormTemplateService(FormTemplateService formTemplateService) {
        this.formTemplateService = formTemplateService;
    }

    public Long getFormTemplateId() {
        return formTemplateId;
    }

    public void setFormTemplateId(Long formTemplateId) {
        this.formTemplateId = formTemplateId;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public boolean isArrprove() {
        return arrprove;
    }

    public void setArrprove(boolean arrprove) {
        this.arrprove = arrprove;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Long getFormId() {
        return formId;
    }

    public void setFormId(Long formId) {
        this.formId = formId;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
}
